package cn.easybuy.web.pre;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.easybuy.entity.Product;
import cn.easybuy.entity.User;
import cn.easybuy.service.proudct.ProductService;
import cn.easybuy.service.proudct.ProductServiceImpl;
import cn.easybuy.utils.EmptyUtils;
import cn.easybuy.utils.MemcachedUtils;
import cn.easybuy.utils.ReturnResult;
import cn.easybuy.web.AbstractServlet;

@WebServlet(urlPatterns= {"/FavoriteServlet"},name="FavoriteServlet")
public class FavoriteServlet extends AbstractServlet {
	
	private ProductService productService;
	
	public void init() throws ServletException {
		productService = new ProductServiceImpl();
	}
	
	public FavoriteServlet() {
		super();
	}
	
	
	/**
	 * 	添加收藏
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws Exception
	 */
	public ReturnResult addFavorite(HttpServletRequest request, HttpServletResponse response) throws ServletException, Exception {
		
		ReturnResult result = new ReturnResult();
		String id = request.getParameter("entityId");
		Product product = productService.findById(id);
		List<Product> favoriteList = queryFavoriteList(request);
		
		//判断是否满了
		if (favoriteList.size() > 0 && favoriteList.size() == 5) {
			favoriteList.remove(0);
		}
		boolean flag = false;
		for (int i = 0; i < favoriteList.size(); i++) {
			//如果收藏的商品是一样的则跳出循环
			if (favoriteList.get(i).getId().equals(product.getId())) {
				flag = true;
				break;
			}
		}
		if (!flag) {
			favoriteList.add(favoriteList.size(),product);
		}
		String key = getFavoriteKey(request);
		MemcachedUtils.add(key, favoriteList);
		return result.returnSuccess();
	}
	
	@Override
	public Class getServletClass() {
		
		return FavoriteServlet.class;
	}
	
	
	/**
	 * 	从Memcached获取已经收藏的product
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public List<Product> queryFavoriteList(HttpServletRequest request) throws Exception{
		String key = getFavoriteKey(request);
		
		List<Product> recentProducts = (List<Product>)MemcachedUtils.get(key);
		if (EmptyUtils.isEmpty(recentProducts)) {
			recentProducts = new ArrayList<Product>();
		}
		return recentProducts;
	}
	
	
	/**
	 * 	获取Memcached的数值
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public String getFavoriteKey(HttpServletRequest request) throws Exception {
		HttpSession session = request.getSession();
		//获取用户对象
		User user = (User)session.getAttribute("loginUser"); 
		//判断用户是否登录
		String key = EmptyUtils.isEmpty(user)?session.getId():user.getLoginName();
		
		return key;
	}
	
	
	public String favoriteList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		List<Product> favoriteList = queryFavoriteList(request);
		request.setAttribute("favoriList", favoriteList);
		
		return "/pre/product/favoriteList";
	}

}
